package com.apkplugin.sample.mainhost;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.apkplugin.android.PluginManager;
import com.apkplugin.android.utils.DLUtils;

public class MainActivity extends Activity implements OnItemClickListener {

	public static final String FROM = "extra.from";
	public static final int FROM_INTERNAL = 0;
	public static final int FROM_EXTERNAL = 1;

	private ArrayList<PluginItem> mPluginItems = new ArrayList<PluginItem>();
	private PluginAdapter mPluginAdapter;

	private ListView mListView;
	private TextView mNoPluginTextView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initData();
		PluginManager.setLogState(true);
	}

	private void initView() {
		mPluginAdapter = new PluginAdapter();
		mListView = (ListView) findViewById(R.id.plugin_list);
		mNoPluginTextView = (TextView) findViewById(R.id.no_plugin);
	}

	private void initData() {
		String pluginFolder = Environment.getExternalStorageDirectory()
				+ "/plugin";
		File file = new File(pluginFolder);
		File[] plugins = file.listFiles();
		if (plugins == null || plugins.length == 0) {
			mNoPluginTextView.setVisibility(View.VISIBLE);
			return;
		}

		for (File plugin : plugins) {
			PluginItem item = new PluginItem();
			item.pluginPath = plugin.getAbsolutePath();
			item.packageInfo = DLUtils.getPackageInfo(this, item.pluginPath);
			mPluginItems.add(item);
		}

		mListView.setAdapter(mPluginAdapter);
		mListView.setOnItemClickListener(this);
		mPluginAdapter.notifyDataSetChanged();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_r:
			System.exit(0);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private class PluginAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public PluginAdapter() {
			mInflater = MainActivity.this.getLayoutInflater();
		}

		@Override
		public int getCount() {
			return mPluginItems.size();
		}

		@Override
		public Object getItem(int position) {
			return mPluginItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.plugin_item, parent,
						false);
				holder = new ViewHolder();
				holder.appIcon = (ImageView) convertView
						.findViewById(R.id.app_icon);
				holder.appName = (TextView) convertView
						.findViewById(R.id.app_name);
				holder.apkName = (TextView) convertView
						.findViewById(R.id.apk_name);
				holder.packageName = (TextView) convertView
						.findViewById(R.id.package_name);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			PluginItem item = mPluginItems.get(position);
			PackageInfo packageInfo = item.packageInfo;
			holder.appIcon.setImageDrawable(DLUtils.getAppIcon(
					MainActivity.this, item.pluginPath));
			holder.appName.setText(DLUtils.getAppLabel(MainActivity.this,
					item.pluginPath));
			holder.apkName.setText(item.pluginPath.substring(item.pluginPath
					.lastIndexOf(File.separatorChar) + 1));
			holder.packageName.setText(packageInfo.applicationInfo.packageName);
			return convertView;
		}
	}

	private static class ViewHolder {
		public ImageView appIcon;
		public TextView appName;
		public TextView apkName;
		public TextView packageName;
	}

	public static class PluginItem {
		public PackageInfo packageInfo;
		public String pluginPath;

		public PluginItem() {
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

		String pp = mPluginItems.get(position).pluginPath;
		PluginManager.setLogState(true);
		boolean a = PluginManager.startPlugin(this, pp);
		if (!a) {
			Toast.makeText(this, "启动" + pp + "插件失败 ", Toast.LENGTH_LONG).show();
		}

	}
}
